<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<table border="" cellspacing="" cellpadding="10">
			<tr>
				<th></th>
				<th>姓名</th>
				<th>年龄</th>
				<th>性别</th>
				<th>家庭住址</th>
			</tr>
			<tr>
				<td><input type="checkbox" name="" id="" value="" /></td>
				<td>张三</td>
				<td>20</td>
				<td>男</td>
				<td>台湾省台北市</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="" id="" value="" /></td>
				<td>张三</td>
				<td>20</td>
				<td>男</td>
				<td>台湾省台北市</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="" id="" value="" /></td>
				<td>张三</td>
				<td>20</td>
				<td>男</td>
				<td>台湾省台北市</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="" id="" value="" /></td>
				<td>张三</td>
				<td>20</td>
				<td>女</td>
				<td>台湾省台北市</td>
			</tr>
			<tr>
				<td><input type="checkbox" name="" id="" value="" /></td>
				<td>张三</td>
				<td>20</td>
				<td>男</td>
				<td>台湾省台北市</td>
			</tr>
		</table>
		<input type="checkbox" name="" id="check" value="" onclick="quanxuan()"/>全选
		<input type="checkbox" name="" id="checknan" value="" onclick="quanxuannan()"/>全选男
		<input type="checkbox" name="" id="checknv" value="" onclick="quanxuannv()"/>全选女
		<button type="button" onclick="fanxuan()">反选</button>
	</body>
	<script type="text/javascript">
		function quanxuan(){
			var a=document.getElementsByTagName('table')[0];
			var b=a.getElementsByTagName('input');
			console.log(b)
			var c=document.getElementById('check').checked
			for(var i=0;i<b.length;i++){
				b[i].checked=c;
			}
			
		}
		
		
		function quanxuannan(){
			var a=document.getElementsByTagName('table')[0];
			var b=a.getElementsByTagName('tr');
			var c=a.getElementsByTagName('input');
			var d = document.getElementById("checknan").checked;
			if(d == false){
				for(var i = 0; i < c.length; i++){
						c[i].checked = false;
				}
			}else{
				for(var i=0;i<b.length;i++){
					if(b[i].innerText.indexOf('男') != -1){
						c[i - 1].checked = true; 
					}
				 }
			}
		}
		
		function quanxuannv(){
			var a=document.getElementsByTagName('table')[0];
			var b=a.getElementsByTagName('tr');
			var c=a.getElementsByTagName('input');
			var d = document.getElementById("checknv").checked;
			if(d == false){
				for(var i = 0; i < c.length; i++){
						c[i].checked = false;
				}
			}else{
				for(var i=0;i<b.length;i++){
					if(b[i].innerText.indexOf('女') != -1){
						c[i - 1].checked = true; 
					}
				 }
			}
		}
	</script>
</html>
